Skip to content

New FontData Constructor to create a deep copy #2128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ShahzaibIbrahim
Copy link
Contributor

This change will give us a chance to use proper channel to create a deep copy of FontData object instead of using FontData(fd.toString)

Copy link
Contributor

github-actions bot commented May 8, 2025

Test Results

   539 files   -  6     539 suites   - 6   39m 48s ⏱️ + 4m 14s
 4 341 tests  - 36   4 325 ✅  - 34   15 💤  - 3  1 ❌ +1 
16 614 runs   - 33  16 475 ✅  - 31  138 💤  - 3  1 ❌ +1 

For more details on these failures, see this check.

Results for commit 46cfcee. ± Comparison against base commit 63048a2.

This pull request removes 37 and adds 1 tests. Note that renamed tests count towards both.
AllWin32Tests org.eclipse.swt.graphics.ImageWin32Tests ‑ testImageDataForDifferentFractionalZoomsShouldBeDifferent
AllWin32Tests org.eclipse.swt.graphics.ImageWin32Tests ‑ testImageShouldHaveDimesionAsPerZoomLevel
AllWin32Tests org.eclipse.swt.tests.win32.Test_org_eclipse_swt_dnd_DND ‑ testByteArrayTransfer
AllWin32Tests org.eclipse.swt.tests.win32.Test_org_eclipse_swt_dnd_DND ‑ testFileTransfer
AllWin32Tests org.eclipse.swt.tests.win32.Test_org_eclipse_swt_dnd_DND ‑ testHtmlTransfer
AllWin32Tests org.eclipse.swt.tests.win32.Test_org_eclipse_swt_dnd_DND ‑ testImageTransfer_fromCopiedImage
AllWin32Tests org.eclipse.swt.tests.win32.Test_org_eclipse_swt_dnd_DND ‑ testImageTransfer_fromImage
AllWin32Tests org.eclipse.swt.tests.win32.Test_org_eclipse_swt_dnd_DND ‑ testImageTransfer_fromImageData
AllWin32Tests org.eclipse.swt.tests.win32.Test_org_eclipse_swt_dnd_DND ‑ testImageTransfer_fromImageDataFromImage
AllWin32Tests org.eclipse.swt.tests.win32.Test_org_eclipse_swt_dnd_DND ‑ testRtfTransfer
…
org.eclipse.swt.tests.junit.Test_org_eclipse_swt_graphics_FontData ‑ test_ConstructorLjava_lang_FontData

♻️ This comment has been updated with latest results.

This change will give us a chance to use proper channel to create a deep
copy of FontData object instead of using FontData(fd.toString)
Using the new constructor FontData(fontData) to deep copy the object.
Copy link
Contributor

@HeikoKlare HeikoKlare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a proper copy constructor for FontData sounds reasonable to me.
However, the implementations are no proper copy constructors. They seem to be copies of the String-based constructors which is only able to copy the data present in the string, while a proper copy constructor can simply deep-copy all the data from the existing FontData object.

Comment on lines +259 to +285
if (fontData.data != null) {
LOGFONT newData = new LOGFONT();
setHeight(fontData.getHeightF());
newData.lfHeight = fontData.data.lfHeight;
newData.lfWidth = fontData.data.lfWidth;
newData.lfEscapement = fontData.data.lfEscapement;
newData.lfOrientation = fontData.data.lfOrientation;
newData.lfWeight = fontData.data.lfWeight;
newData.lfItalic = fontData.data.lfItalic;
newData.lfUnderline = fontData.data.lfUnderline;
newData.lfStrikeOut = fontData.data.lfStrikeOut;
newData.lfCharSet = fontData.data.lfCharSet;
newData.lfOutPrecision = fontData.data.lfOutPrecision;
newData.lfClipPrecision = fontData.data.lfClipPrecision;
newData.lfQuality = fontData.data.lfQuality;
newData.lfPitchAndFamily = fontData.data.lfPitchAndFamily;

// Deep copy of the char array lfFaceName
if (fontData.data.lfFaceName != null) {
newData.lfFaceName = new char[fontData.data.lfFaceName.length];
System.arraycopy(fontData.data.lfFaceName, 0, newData.lfFaceName, 0, fontData.data.lfFaceName.length);
}

this.data = newData;
} else {
this.data = null;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be placed in a dedicated private static cloneLogfont method, in which you can also early return on that font being null.


if (fontData.data != null) {
LOGFONT newData = new LOGFONT();
setHeight(fontData.getHeightF());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of setting specific fields via setters, shouldn't we copy over all data in a proper copy constructor, i.e., also the height, lang, country and variant fields? This seems to be a copy-over from the String-based constructor, in which some original data is missing and needs to be re-initialized from the data being given using according setters.

* </ul>
* @since 3.130
*/
public FontData(FontData fontData) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as for the other implementations: why not simply copy the fields from the original font data instead of using setters with additional checks that may not be useful when already having a properly initialized font data hand? In addition, some data like lang, country, and variant seem to not be copied by the copy constructors.

@@ -186,6 +186,26 @@ public FontData(String string) {
}
}

/**
* Constructs a new FontData copy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Constructs a new FontData copy
* Constructs a deep copy of the given font data object.

/**
* Constructs a new FontData copy
*
* @param fontData the FondData object for which copy is needed to be made
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param fontData the FondData object for which copy is needed to be made
* @param fontData the FontData object to copy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve FontData Copying: Replace String Constructor with Safer Approach
2 participants